package com.example.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

/**
 * @author chenchao
 *
 */
@Entity
@Table(name="t_order")
public class Order {
    @Id
    private String orderNum;
    private double money;
    private String recivePersion;
    private String recivePhone;
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true)
    @JoinColumn(name="orderNum", referencedColumnName="orderNum")
    @OrderBy("id")
    private Set<OrderItem> orderItems = new HashSet<>();

    public Order() {
        super();
    }


    public Order(String orderNum) {
        super();
        this.orderNum = orderNum;
    }

    public Order(String orderNum, double money) {
        super();
        this.orderNum = orderNum;
        this.money = money;
    }
    public Order(String orderNum, double money, String recivePersion, String recivePhone) {
        super();
        this.orderNum = orderNum;
        this.money = money;
        this.recivePersion = recivePersion;
        this.recivePhone = recivePhone;
    }
    public String getOrderNum() {
        return orderNum;
    }
    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    public String getRecivePersion() {
        return recivePersion;
    }
    public void setRecivePersion(String recivePersion) {
        this.recivePersion = recivePersion;
    }
    public String getRecivePhone() {
        return recivePhone;
    }
    public void setRecivePhone(String recivePhone) {
        this.recivePhone = recivePhone;
    }

    public Set<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(Set<OrderItem> orderItems) {
        this.orderItems.clear();
        this.orderItems.addAll(orderItems);
    }

    public void addOrderItem(OrderItem orderItem) {
        this.orderItems.add(orderItem);
    }

    @Override
    public String toString() {
        return String.format("Order [orderNum=%s, money=%s, recivePersion=%s, recivePhone=%s, orderItems=%s]", orderNum,
                money, recivePersion, recivePhone, orderItems);
    }

}
